package com.woniu.hospital.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.api.domain.SysDept;
import com.ruoyi.system.api.domain.SysUser;
import com.woniu.hospital.domain.Bedinfo;
import com.woniu.hospital.domain.Hosptilnews;
import com.woniu.hospital.service.*;
import com.woniu.hospital.service.impl.BedinfoServiceImpl;
import com.woniu.hospital.vo.HosptilnewsVo;
import com.woniu.hospital.vo.InpatientVo;
import com.woniu.user.domain.Dept;
import com.woniu.user.domain.Doctor;
import com.woniu.user.domain.Patient;
import com.woniu.user.vo.DeptVo;
import com.woniu.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * 住院信息表Controller
 * 
 * @author wolf-six
 * @date 2023-02-15
 */
@RestController
@RequestMapping("/hosptilnews")
public class HosptilnewsController extends BaseController {
    @Autowired
    private IHosptilnewsService hosptilnewsService;
    @Autowired
    private IDoctorService iDoctorService;
    @Autowired
    private IPatientService iPatientService;
    @Autowired
    private IBedinfoService ibedinfoService;
    @Autowired
    private IDeptService iDeptService;
    @Autowired
    private IChecksService iChecksService;
    @Autowired
    private ISurgicalDescService iSurgicalDescService;
    @Autowired
    private IPrescriptionService iPrescriptionService;
    @Autowired
    private IDischargeService iDischargeService;
    /**
     * 查询住院信息表列表
     */
    @RequiresPermissions("hospital:hosptilnews:list")
    @GetMapping("/list")
    public TableDataInfo list(Hosptilnews hosptilnews) {
        startPage();
        List<HosptilnewsVo> hosptilnewsVos = new ArrayList<>();

        List<Hosptilnews> list = hosptilnewsService.selectHosptilnewsList(hosptilnews);
        for (Hosptilnews hosptilnew : list) {
            Doctor doctor = iDoctorService.selectById(hosptilnew.getDocterId());
            Doctor doctor1 = iDoctorService.selectById(hosptilnew.getDocter2Id());
            Patient patient = iPatientService.selectById(hosptilnew.getPatientId());
            Bedinfo bedinfo = ibedinfoService.selectBedinfoByBedId(hosptilnew.getBedId());
            Dept dept = iDeptService.selectById(hosptilnew.getDeptId());


            HosptilnewsVo hosptilnewsVo = new HosptilnewsVo();
            hosptilnewsVo.setHosptilId(hosptilnew.getHosptilId());
            hosptilnewsVo.setHosptilIdcard(hosptilnew.getHosptilIdcard());
            hosptilnewsVo.setHosptilPatientadd(hosptilnew.getHosptilPatientadd());
            hosptilnewsVo.setContactPerson1(hosptilnew.getContactPerson1());
            hosptilnewsVo.setHosptilStatus(hosptilnew.getHosptilStatus());
            hosptilnewsVo.setHosptilIntime(hosptilnew.getHosptilIntime());
            hosptilnewsVo.setHosptilOuttime(hosptilnew.getHosptilOuttime());
            hosptilnewsVo.setPatientName(patient.getPatientName());
            hosptilnewsVo.setDocterName(doctor.getDoctorName());
            hosptilnewsVo.setDocterName2(doctor1.getDoctorName());
            hosptilnewsVo.setBedName(bedinfo.getBedName());
            hosptilnewsVo.setDeptName(dept.getDeptName());

            hosptilnewsVos.add(hosptilnewsVo);
        }
        return getDataTable(hosptilnewsVos);
    }

    /**
     * 导出住院信息表列表
     */
    @RequiresPermissions("hospital:hosptilnews:export")
    @Log(title = "住院信息表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Hosptilnews hosptilnews)
    {
        List<Hosptilnews> list = hosptilnewsService.selectHosptilnewsList(hosptilnews);
        ExcelUtil<Hosptilnews> util = new ExcelUtil<Hosptilnews>(Hosptilnews.class);
        util.exportExcel(response, list, "住院信息表数据");
    }

    /**
     * 获取住院信息表详细信息
     */
    @RequiresPermissions("hospital:hosptilnews:query")
    @GetMapping(value = "/{hosptilId}")
    public Result getInfo(@PathVariable("hosptilId") Long hosptilId) {
        Hosptilnews hosptilnews = hosptilnewsService.selectHosptilnewsByHosptilId(hosptilId);
        Long deptId = hosptilnews.getDeptId();
        List<Doctor> doctor1 = iDoctorService.filtrate1(deptId);//医生
        List<Doctor> doctor2 = iDoctorService.filtrate2(deptId);//护士
        String deptName = iDeptService.selectById(deptId).getDeptName();//部门名字
        List<Bedinfo> bedinfo = ibedinfoService.selectBydeptName(deptName);//根据部门名字查床位

        return Result.ok().data("xx",hosptilnews).data("doctorList",doctor1).data("nurseList",doctor2).data("bedList",bedinfo);

    }

    /**
     * 新增住院信息表
     */
    @RequiresPermissions("hospital:hosptilnews:add")
    @Log(title = "住院信息表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Hosptilnews hosptilnews) {
        return toAjax(hosptilnewsService.insertHosptilnews(hosptilnews));
    }

    @PostMapping("/selOne")
    public Result selOne(@RequestBody Hosptilnews hosptilnews){
        System.out.println(hosptilnews);
        Patient patient = iPatientService.selectById(hosptilnews.getPatientId());
        InpatientVo inpatientVo = new InpatientVo();
        inpatientVo.setPatientName(patient.getPatientName());
        inpatientVo.setDickerSex(patient.getDickerSex());
        inpatientVo.setPatientIdcard(patient.getPatientIdcard());
        inpatientVo.setPatientAge(patient.getPatientAge());
        inpatientVo.setDoctorTel(patient.getDoctorTel());
        inpatientVo.setHosptilPatientadd(hosptilnews.getHosptilPatientadd());
        inpatientVo.setContactPerson1(hosptilnews.getContactPerson1());
        String name1 = iDoctorService.selectById(hosptilnews.getDocterId()).getDoctorName();
        inpatientVo.setDocterName(name1);
        String name2 = iDoctorService.selectById(hosptilnews.getDocter2Id()).getDoctorName();
        inpatientVo.setDocter2Name(name2);
        String bedName = ibedinfoService.selectBedinfoByBedId(hosptilnews.getBedId()).getBedName();
        inpatientVo.setBedName(bedName);
        String deptName = iDeptService.selectById(hosptilnews.getDeptId()).getDeptName();
        inpatientVo.setDeptName(deptName);
        System.out.println(inpatientVo);
        return Result.ok().data("inpatient",inpatientVo);
    }

    /**
     * 修改住院信息表
     */
    @RequiresPermissions("hospital:hosptilnews:edit")
    @Log(title = "住院信息表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Hosptilnews hosptilnews)
    {
        return toAjax(hosptilnewsService.updateHosptilnews(hosptilnews));
    }

    /**
     * 删除住院信息表
     */
    @RequiresPermissions("hospital:hosptilnews:remove")
    @Log(title = "住院信息表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{hosptilIds}")
    public AjaxResult remove(@PathVariable Long[] hosptilIds) {
        return toAjax(hosptilnewsService.deleteHosptilnewsByHosptilIds(hosptilIds));
    }

    /**
     * 出院
     */
    @DeleteMapping("/goOut/{hosptilIds}")
    public AjaxResult remove2(@PathVariable Long hosptilIds) {
        Long patientId = hosptilnewsService.selectHosptilnewsByHosptilId(hosptilIds).getPatientId();
        BigDecimal cost1 = iChecksService.checkAmount(patientId);
        BigDecimal cost2 = iSurgicalDescService.surgeryCost(patientId);
        BigDecimal cost3 = iPrescriptionService.drugCost(patientId);
        iDischargeService.dischargeCost(patientId,cost1,cost2,cost3);
        return toAjax(hosptilnewsService.goOut(hosptilIds));
    }

    /**
     * 查询所有的科室
     */
    @GetMapping("/selAllDept")
    public Result selAllDept() {
        List<DeptVo> deptList = iDeptService.selAllDept();
        return Result.ok().data("deptList", deptList);
    }

    /**
     * 根据科室选择对应的医生、护士、床位
    */
    @GetMapping("/list2/{deptId}")
    public Result filtrate(@PathVariable("deptId") Long deptId) {
        System.out.println(deptId);
        List<Doctor> doctor1 = iDoctorService.filtrate1(deptId);//医生
        List<Doctor> doctor2 = iDoctorService.filtrate2(deptId);//护士
        String deptName = iDeptService.selectById(deptId).getDeptName();//部门名字
        List<Bedinfo> bedinfo = ibedinfoService.selectBydeptName(deptName);//根据部门名字查床位

        return Result.ok().data("doctorList",doctor1).data("nurseList",doctor2).data("bedList",bedinfo);
    }
}
